package com.gin.kotlin.no_gay_alliance.adapter

import android.content.Context
import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.activity.TopicListActivity
import com.gin.kotlin.no_gay_alliance.app.App
import com.gin.kotlin.no_gay_alliance.argument.TopicListArgument
import com.gin.kotlin.no_gay_alliance.argument.TopicListArgument.Type
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingRecyclerViewAdapter
import com.gin.kotlin.no_gay_alliance.base.GlideModule.Companion.CROSS_FADE
import com.gin.kotlin.no_gay_alliance.base.MyHandler
import com.gin.kotlin.no_gay_alliance.base.TextViewTarget
import com.gin.kotlin.no_gay_alliance.databinding.ItemSubForumBinding
import com.gin.kotlin.no_gay_alliance.model.SubForumModel
import com.gin.nga.callback.NgaCallback
import com.gin.nga.enums.ForumType
import com.gin.nga.enums.status.ForumStatus
import com.gin.nga.response.base.NgaResponse
import retrofit2.Call
import retrofit2.Response

/**
 * 子版面适配器
 * @property forumId 版面ID
 * @constructor
 */
class SubForumAdapter(context: Context, handler: MyHandler, data: MutableList<SubForumModel> = mutableListOf()) :
    AbstractBindingRecyclerViewAdapter<ItemSubForumBinding, SubForumModel>(context, handler, true, data) {
    var forumId: Long = 0

    override fun onBindViewHolder(holder: ViewHolder<ItemSubForumBinding>, position: Int, b: ItemSubForumBinding, dataModel: SubForumModel) {
        // 图标大小（dp）
        val size = (64 * context.resources.displayMetrics.density).toInt()

        b.tvForumName.text = dataModel.name
        // 加载版面头像
        Glide.with(context)
            .load(dataModel.icon)
            .error(R.drawable.ic_forum_avatar)
            .placeholder(R.drawable.ic_forum_avatar)
            .transition(DrawableTransitionOptions.withCrossFade(CROSS_FADE))
            .into(TextViewTarget(b.tvForumName, 0, size, size))

        b.tvForumName.setOnClickListener {
            val type = if (dataModel.type == ForumType.COL) Type.COL_LIST else Type.FORUM_LIST
            TopicListActivity.startActivity(context, TopicListArgument(type, forumId = dataModel.forumId(), colTid = dataModel.colTid()))
        }

        //根据屏蔽状态设置开关初始状态
        b.swBlockSubForum.isChecked = dataModel.getStatus()?.contains(ForumStatus.SELECTED_EXTRA) == true
        b.swBlockSubForum.visibility = if (dataModel.getStatus()?.contains(ForumStatus.SELECTABLE) == true) View.VISIBLE else View.GONE
        b.swBlockSubForum.setOnCheckedChangeListener { buttonView, isChecked ->
            // 暂时禁用开关
            buttonView.isEnabled = false
            // 回调
            val callback = object : NgaCallback<List<String>> {
                override fun onSuccess(
                    call: Call<NgaResponse<List<String>>>,
                    response: Response<NgaResponse<List<String>>>,
                    body: NgaResponse<List<String>>?,
                    data: List<String>?
                ) {
                    buttonView.isEnabled = true
                    data?.get(0)?.also { handler.toast(it) }
                }

                override fun onError(call: Call<NgaResponse<List<String>>>, response: Response<NgaResponse<List<String>>>, body: NgaResponse<List<String>>?, errorMessage: String) {
                    // 请求失败，回滚开关状态
                    buttonView.isEnabled = true
                    handler.toastLong(errorMessage)
                    buttonView.isChecked = !isChecked
                }
            }
            // 发送请求
            val api = App.INSTANCE.ngaApi.forumApi
            val mirrorId = dataModel.mirrorId!!
            if (isChecked)
                api.blockSubForumDel(forumId, mirrorId).enqueue(callback)
            else
                api.blockSubForumAdd(forumId, mirrorId).enqueue(callback)
        }
    }
}